<?php
/**
 * Created by PhpStorm.
 * User: 17828
 * Date: 2018/10/17
 * Time: 17:32
 */
namespace app\master\controller;

use think\Db;
use think\Exception;

class Lease extends Base
{
    //临时约车审核
    public function temporaryContract()
    {
        if (request()->isGet()) {
            return $this->fetch('temporarycontract');
        }
        $field = "m.member_name,m.member_sex,m.member_tel,cp.company_name,c.contractcar_type,from_unixtime(c.contractcar_bigontime,'%Y.%m.%d %H:%i:%s') as contractcar_bigontime,ca.car_name,ca.car_plate,s.store_name,st.store_name as s_store_name,c.contractcar_id,from_unixtime(c.contractcar_endtime,'%Y.%m.%d %H:%i:%s') as contractcar_endtime";
        $where = [
            'c.contractcar_status'=>1,
            'c.type'=>2,
        ];
        $page = input('page');
        $limit = input('limit');
        $list = model('Contractcar')->getTemporaryContract($field,$where,$page,$limit);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);

    }
    //同意审核
    public function agree(){
        $this->view->engine->layout(false);
        $id = input('id');
        if(request()->isGet()){
            $drive = db('drive')->where(['drive_status'=>1,'drive_isgo'=>2])->order('drive_id desc')->select();
            $this->assign('id',$id);
            $this->assign('drive',$drive);
            return $this->fetch('agree');
        }
        $is_need = input('is_need');
        $drive_id = input('drive_id');
        $find = db('contractcar')->where('contractcar_id',$id)->find();
        if($find['contractcar_status'] != 1) $this->ajaxReturn(603,'系统繁忙，请稍后再试！');
        $data = [
            'contractcar_status'=>2,
        ];
        if($is_need == 1){
            if(!$drive_id) $this->ajaxReturn(603,'请选择驾驶员！');
            $data['drive_id'] = $drive_id;
            db('drive')->where('drive_id',$drive_id)->update(['drive_isgo'=>1,'company_id'=>$find['company_id']]);
        }
        db('contractcar')->where('contractcar_id',$id)->update($data);
        $this->ajaxReturn(200,'操作成功');
    }
    //拒绝审核
    public function refuse(){
        $id = input('id');
        $find = db('contractcar')->where('contractcar_id',$id)->find();
        if($find['contractcar_status'] != 1) $this->ajaxReturn(603,'系统繁忙，请稍后再试！');
        $res = db('contractcar')->where('contractcar_id',$id)->update(['contractcar_status'=>3]);
        if($res) $this->ajaxReturn(200,'操作成功');
        else $this->ajaxReturn(603,'操作失败');
    }
    //临时约车审核记录
    public function temporaryContractLog()
    {
        if (request()->isGet()) {
            return $this->fetch('temporarycontractlog');
        }
        $page = input('page');
        $limit = input('limit');
        $field = "m.member_name,m.member_tel,co.contractcar_company,from_unixtime(co.contractcar_bigontime,'%Y.%m.%d %H:%i:%s') as contractcar_bigontime,co.contractcar_type,c.car_plate,d.drive_name,d.drive_tel,co.contractcar_status,co.contractcar_id,m.member_sex,from_unixtime(co.contractcar_endtime,'%Y.%m.%d %H:%i:%s') as contractcar_endtime";
        $where = [
            'co.contractcar_status'=>['not in',1],
            'co.type'=>2,
        ];
        $list = model('Contractcar')->getTemporaryContractLog($field,$where,$page,$limit);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }

    //派车
    public function sendCar(){

    }
    //派驾驶员
    public function dispatchedDriver(){

    }
    //派车记录
    public function sendCarLog(){

    }
    //派驾驶员记录
    public function dispatchedDriverLog(){

    }
}